package Classic150;

public class 多数元素 {
    /*
        遍历存入hashmap中计数取值最大的键 || 排序后取n/2下标的元素 || Boyer-Moore 算法
     */
    public int majorityElement(int[] nums) {
        int winner = nums[0];
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == winner) count++;
            else if (count == 0) {
                winner = nums[i];
                count++;
            } else count--;
        }
        return winner;
    }
}
